package hhm.javamode.iterator.phone;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class ContainerImpl implements Container {
	private List<Object> list = new ArrayList<>();
	private Class<?> clazz;

	public void setPlan(Class<?> clazz) {
		this.clazz = clazz;
	}

	public void add(Object obj) {
		this.list.add(obj);
	}

	public void remove(Object obj) {
		this.list.remove(obj);
	}

	public Object get(int index) {
		if (index <= (this.list.size() - 1)) {
			return this.list.get(index);
		}
		return null;
	}

	@Override
	public Iterator iterator() {
		try {
			Constructor<?> con = clazz.getConstructor(List.class);
			return (Iterator) con.newInstance(list);
		} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;

	}

}
